//
// Created by hzq on 2022/6/7.
//
#include "awi_audiomix_process.h"
#include "string.h"
#include "math.h"

#define ALGO_FACTOR  32768

float sum_abs(float *input, int size_vec)
{
    float sum_abs_tmp = 0;
    for (int i = 0; i< size_vec; i++)
    {
        sum_abs_tmp += fabs(input[i]);
    }
    return sum_abs_tmp;
}


float mean_vec(float *input, int size_vec)
{
    float mean_tmp = 0;
    for (int i = 0; i < size_vec; i++)
    {
        mean_tmp += input[i];
    }
    mean_tmp = mean_tmp/ size_vec;

    return mean_tmp;

}

void awi_audio_mixer(audiomix_cfg_t *p, float *input_frame1, float *input_frame2, int frame_size, float *out_mixed_frame)
{
    float factor = 1e-20f;

    float gain_frame1_rcd[AWI_ITERATION_FRAMES] = {0};
    float gain_frame2_rcd[AWI_ITERATION_FRAMES] = {0};
    // float gain_frame3_rcd[AWI_ITERATION_FRAMES];

    float gain_frame1_rcd_new[AWI_ITERATION_FRAMES] = {0};
    float gain_frame2_rcd_new[AWI_ITERATION_FRAMES] = {0};
    // float gain_frame3_rcd_new[AWI_ITERATION_FRAMES];


    

    float sum_amp1 = 0; 
    float sum_amp2 = 0; 
    // float sum_amp3 = 0;
    float sum_amp_full = 0;

    float ratio1 = 0; 
    float ratio2 = 0; 
    // float ratio3 = 0;

    memcpy(gain_frame1_rcd, p->gain_frame1_rcd, AWI_ITERATION_FRAMES * sizeof(float));
    memcpy(gain_frame2_rcd, p->gain_frame2_rcd, AWI_ITERATION_FRAMES * sizeof(float));
    // memcpy(gain_frame3_rcd, p->gain_frame3_rcd, AWI_ITERATION_FRAMES * sizeof(float));


    sum_amp1 = sum_abs(input_frame1, frame_size);
    sum_amp2 = sum_abs(input_frame2, frame_size);
    // sum_amp3 = sum_abs(input_frame3, frame_size);

    sum_amp_full = sum_amp1 + sum_amp2;

 
    ratio1 = (sum_amp1 + factor) / (sum_amp_full + factor);
    ratio2 = (sum_amp2 + factor) / (sum_amp_full + factor);
    // ratio3 = (sum_amp3 + factor) / (sum_amp_full + factor);

    // update
    memcpy(gain_frame1_rcd_new, gain_frame1_rcd + 1, (AWI_ITERATION_FRAMES -1) * sizeof(float));
    gain_frame1_rcd_new[AWI_ITERATION_FRAMES -1] = ratio1;

    memcpy(gain_frame2_rcd_new, gain_frame2_rcd + 1, (AWI_ITERATION_FRAMES -1) * sizeof(float));
    gain_frame2_rcd_new[AWI_ITERATION_FRAMES -1] = ratio2;

    // memcpy(gain_frame3_rcd_new, gain_frame3_rcd + 1, (AWI_ITERATION_FRAMES -1) * sizeof(float));
    // gain_frame3_rcd_new[AWI_ITERATION_FRAMES -1] = ratio3;

    float gain1_inst = mean_vec(gain_frame1_rcd, AWI_ITERATION_FRAMES);
    float gain2_inst = mean_vec(gain_frame2_rcd, AWI_ITERATION_FRAMES);
    // float gain3_inst = mean_vec(gain_frame3_rcd, AWI_ITERATION_FRAMES);
    memcpy( p->gain_frame1_rcd, gain_frame1_rcd_new, AWI_ITERATION_FRAMES * sizeof(float));
    memcpy( p->gain_frame2_rcd, gain_frame2_rcd_new, AWI_ITERATION_FRAMES * sizeof(float));

    // MIX
    for (int i = 0; i < frame_size; i++)
    {
        // out_mixed_frame[i] = gain1_inst * input_frame1[i] + gain2_inst * input_frame2[i] + gain3_inst * input_frame3[i];
        out_mixed_frame[i] = gain1_inst * input_frame1[i] + gain2_inst * input_frame2[i];
    }
}

void awi_audio_mixer_process(audiomix_cfg_t *p, short *input_frame1, short *input_frame2, int frame_size, short *output)
{   
    int i = 0;
    float algoFactor = 1.0/ALGO_FACTOR;
    
    int dataLen = AWI_FRAME_SIZE;
    float inputFrame1[AWI_FRAME_SIZE] = {0};
    float inputFrame2[AWI_FRAME_SIZE] = {0};
    float outputFrame[AWI_FRAME_SIZE] = {0};

    for(i = 0; i < dataLen; ++i)
    {
        inputFrame1[i] = algoFactor * input_frame1[i];
    }

    for(i = 0; i < dataLen; ++i)
    {
        inputFrame2[i] = algoFactor * input_frame2[i] ;
    }

    awi_audio_mixer(p, inputFrame1, inputFrame2, frame_size, outputFrame);

    for(i = 0; i < dataLen; ++i)
    {
        output[i] = ALGO_FACTOR * outputFrame[i];
    }

}



